package com.baidu.yun.channel.sample;

import com.baidu.yun.channel.auth.ChannelKeyPair;
import com.baidu.yun.channel.client.BaiduChannelClient;
import com.baidu.yun.channel.exception.ChannelClientException;
import com.baidu.yun.channel.exception.ChannelServerException;
import com.baidu.yun.channel.model.PushTagMessageRequest;
import com.baidu.yun.channel.model.PushTagMessageResponse;
import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;

public class AndroidPushTagMessageSample {

    public static void main(String[] args) {
    	String title ="hello";
    	String description ="22";
    	String custom_content ="{'id':'014年元旦、春节、清明节、劳动节、端午节、中秋节和国庆节放假调休日期的具体安排通知如下。一、元旦：1月1日放假1天。二、春节：1月31日至2月6日放假调休，共7天。1月26日（星期日）、2月8日（星期六）上班。三、清明节：4月5日放假，4月7日（星期一）补休。四、劳动节：5月1日至3日放假调休，共3天。5月4日（星期日）上班。五、端午节：6月2日放假，与周末连休。'}";
    	new AndroidPushTagMessageSample().pushMessage(title, description, custom_content);


    }
   public  static void pushMessage(String title,String description,String custom_content){
       // 1. 设置developer平台的ApiKey/SecretKey
       String apiKey = Config.apiKey;
       String secretKey = Config.secretKey;
       ChannelKeyPair pair = new ChannelKeyPair(apiKey, secretKey);

       // 2. 创建BaiduChannelClient对象实例
       BaiduChannelClient channelClient = new BaiduChannelClient(pair);

       // 3. 若要了解交互细节，请注册YunLogHandler类
       channelClient.setChannelLogHandler(new YunLogHandler() {
           @Override
           public void onHandle(YunLogEvent event) {
               System.out.println(event.getMessage());
           }
       });

       try {

           // 4. 创建请求类对象
           PushTagMessageRequest request = new PushTagMessageRequest();
           request.setDeviceType(3); // device_type => 1: web 2: pc 3:android
                                     // 4:ios 5:wp
           request.setTagName("all_message");
           // 若要通知，
          request.setMessageType(1);
          title="\""+title+"\""; //加上双引号
          description="\""+description+"\""; //加上双引号
          // custom_content="\""+custom_content+"\""; //加上双引号
          String json = "{\"title\":"+title+",\"description\":"+description+",\"custom_content\":"+custom_content+"}";
          request.setMessage("{\"title\":"+title+",\"description\":"+description+",\"custom_content\":"+custom_content+"}");
           // 5. 调用pushMessage接口
           PushTagMessageResponse response = channelClient
                   .pushTagMessage(request);

           // 6. 认证推送成功
           System.out.println("push amount : " + response.getSuccessAmount());

       } catch (ChannelClientException e) {
           // 处理客户端错误异常
           e.printStackTrace();
       } catch (ChannelServerException e) {
           // 处理服务端错误异常
           System.out.println(String.format(
                   "request_id: %d, error_code: %d, error_message: %s",
                   e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
       }
   }

}
